/*
所有公共方法写入这个里面
*/

// eliminate 消除 null undefined ''
const eliminate = function (val,defaultVal){
	const valList = ['', null, undefined, 'null', 'undefined'];
	
	if(!valList.includes(val)) return val;
	if(!defaultVal) return '暂无数据';
	return defaultVal;
}

const getPhoneInfo = function(){
	return new Promise(
		function(resolve,reject){
			uni.getSystemInfo({
			    success: function (res) {
					resolve(res)
			    }
			});
		}
	)}
	
// url转base64 
const urlToBase64 = function(url){
      return new Promise ((resolve,reject) => {
        let image = new Image();
        image.onload = function() {
          let canvas = document.createElement('canvas');
          canvas.width = this.naturalWidth;
          canvas.height = this.naturalHeight;
          // 将图片插入画布并开始绘制
          canvas.getContext('2d').drawImage(image, 0, 0);
          // result
          let result = canvas.toDataURL('image/png')
          resolve(result);
        };
        // CORS 策略，会存在跨域问题https://stackoverflow.com/questions/20424279/canvas-todataurl-securityerror
        image.setAttribute("crossOrigin",'Anonymous');
        image.src = url;
        // 图片加载失败的错误处理
        image.onerror = () => {
          reject(new Error('urlToBase64 error'));
      };
    })
	}
export {
	eliminate,
	getPhoneInfo,
	urlToBase64
}